package com.dk.controller;

import com.dk.data.PageBean;
import com.dk.data.Result;
import com.dk.em.ConstantEnum;
import com.dk.entity.TGoods;
import com.dk.mapper.TGoodsMapper;
import com.dk.service.ITGoodsService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2021-11-01
 */
@RestController
@RequestMapping("/goods")
public class TGoodsController {

    @Autowired
    private TGoodsMapper goodsMapper;

    @Autowired
    private ITGoodsService goodsService;

//    后台自营商品增加
    @ApiOperation(value = "后台自营商品增加功能")
    @ApiImplicitParams({
            @ApiImplicitParam(name="name",value = "商品名称",required = true,paramType = "query"),
            @ApiImplicitParam(name="num",value = "库存",required = true,paramType = "query")
    })
    @PostMapping("add")
    public Result add(TGoods goods){
        int i = goodsMapper.insert(goods);
        if (i>0){
            return Result.bulid(ConstantEnum.SUCCESS).setResult("自营商品增加成功");
        }
        return Result.bulid(ConstantEnum.ERROR).setResult("自营商品增加失败");
    }
    //    后台自营商品修改
    @ApiOperation(value = "后台自营商品修改功能")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "商品id",required = true,paramType = "query"),
            @ApiImplicitParam(name="name",value = "商品名称",required = true,paramType = "query"),
            @ApiImplicitParam(name="num",value = "库存",required = true,paramType = "query")
    })
    @PostMapping("update")
    public Result update(TGoods goods){
        int i = goodsMapper.updateById(goods);
        if (i > 0){
            return Result.bulid(ConstantEnum.SUCCESS).setResult("自营商品修改成功");
        }
        return Result.bulid(ConstantEnum.ERROR).setResult("自营商品修改失败");
    }
    //    后台自营商品删除
    @ApiOperation(value = "后台自营商品单条删除功能")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "商品id",required = true,paramType = "query"),
    })
    @PostMapping("deleteById")
    public Result deleteById(Long id){
        int i = goodsMapper.deleteById(id);
        if (i > 0){
            return Result.bulid(ConstantEnum.SUCCESS).setResult("自营商品单条删除成功");
        }
        return Result.bulid(ConstantEnum.ERROR).setResult("自营商品单条删除失败");
    }
    //    后台自营商品批量删除
    @ApiOperation(value = "后台自营商品批量删除功能")
    @ApiImplicitParams({
            @ApiImplicitParam(name="ids",value = "商品id数组",required = true,paramType = "query")
    })
    @RequestMapping(value = "deleteByIds",method = RequestMethod.DELETE)
    public Result deleteByIds(@RequestParam Long[] ids){
        boolean b = goodsService.removeByIds(Arrays.asList(ids));
        if (b){
            return Result.bulid(ConstantEnum.SUCCESS).setResult("自营商品批量删除成功");
        }
        return Result.bulid(ConstantEnum.ERROR).setResult("自营商品批量删除失败");
    }
    //    查询后台自营商品表
    @ApiOperation(value = "查询后台自营商品表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="num",value = "从第几条开始",required = true,paramType = "query"),
            @ApiImplicitParam(name="size",value = "查询多少条",required = true,paramType = "query")
    })
    @GetMapping("findAllByGoods")
    public Result findAllByGoods(Integer num,Integer size){
        PageBean pageBean=goodsService.findAllByGoods(num,size);
        return Result.bulid(ConstantEnum.SUCCESS).setResult(pageBean);
    }
}
